package com.northpool.operator.common.spatial;

import com.northpool.geometry.Operator;
import com.northpool.geometry.OperatorFactoryLocal;
import com.northpool.geometry.OperatorImportFromGeoJson;
import com.northpool.geometry.Polygon;
import com.northpool.geometry.ogc.OGCGeometry;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.utils.StpOperatorHelper;
import com.northpool.spatial.Geom;

@OperatorInfo(ref = "Geojson2WktOperator", name = "GEOJSON转WKT")
@OutputParams(
        @OutputParam(ref = "wkt", name = "WKT")
)
public class Geojson2WktOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "geojson", name = "GeoJson", desc = "GeoJson字符串，如: {\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{},\"geometry\":{\"coordinates\":[121.04327319368122,31.272014518631494],\"type\":\"Point\"}}]}", required = true)
    })
    public Geojson2WktOperator() {

    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(Geojson2WktOperator.class, TestParamBuilder.aBuilder()
                .param("geojson", "{\"type\":\"Polygon\",\"coordinates\":[[[113.2254028787842,28.41949496288678],[113.22539841821425,28.419617823789586],[113.2254892341455,28.419859559837647],[113.22548971717715,28.419861057240386],[113.22555648899214,28.420108344829643],[113.22555679422271,28.42010970510596],[113.22558904524718,28.420289307149194],[113.22564377399875,28.420393330810366],[113.225645258839,28.420397296205234],[113.22568139523108,28.420552195862513],[113.22568147175187,28.42055255202868],[113.225726884035,28.420769094150664],[113.22572697748805,28.42076959992016],[113.22575961910852,28.420954324864567],[113.22582121703259,28.42125163722163],[113.22583667917095,28.42128345710486],[113.22582414599398,28.42128575515202],[113.22579879606668,28.421291955006794],[113.225796973805,28.42129318087877],[113.2257828002297,28.421264012931708],[113.22578147616392,28.421260141912033],[113.22571941609355,28.420960594127333],[113.22571933011972,28.420960127571963],[113.22568669195405,28.420775422763757],[113.22564137209136,28.42055930993621],[113.22560575702425,28.42040664491163],[113.22555082711868,28.42030223979215],[113.22554917499869,28.420297458046317],[113.22551659917573,28.420116048856826],[113.22545021419042,28.419870195807796],[113.22535853079415,28.419626146778036],[113.22535750025014,28.419619880880802],[113.22536198995957,28.419496228917858],[113.22538670200406,28.41949921413419],[113.2254028787842,28.41949496288678]]],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"EPSG:4326\"}}}")
                .build()
        );
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String geojson = paramTool.getStr("geojson");

        OperatorImportFromGeoJson importerGeoJson = (OperatorImportFromGeoJson) OperatorFactoryLocal.getInstance().getOperator(Operator.Type.ImportFromGeoJson);
        Polygon polygon = (Polygon) (importerGeoJson.execute(0, com.northpool.geometry.Geometry.Type.Unknown, geojson, null).getGeometry());
        Geom geom = new Geom(com.northpool.spatial.Constants.SPATIAL_TYPE.esriGeometry
                , OGCGeometry.createFromEsriGeometry(polygon, null));

        this.result("wkt", geom.toEsriGeometry().asText());
    }
}
